<?php
namespace Custom\ExamBundle\Controller;

use Topxia\Service\Common\ServiceKernel;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Topxia\WebBundle\Controller\BaseController;
use Topxia\Common\ArrayToolkit;

class DesignController extends BaseController
{
	public function addQuestionAction(Request $request, $testpaperId)
	{
		$testpaper = $this->getTestpaperService()->getTestpaper($testpaperId);
		if (empty($testpaper)) {
			throw $this->createNotFoundException();
		}

		if ($request->getMethod() == 'POST') {
			$data = $request->request->all();
			
			$questionIds =  explode ( ",", $data["qid"] );
			$items = array();
			foreach ($questionIds as $questionId) {
				$items[] = array('questionId' => $questionId, 'score' => 0);
				
   				//添加子题
 				$question = $this->getQuestionExtService()->getQuestion($questionId);
				if ($question['subCount'] > 0) {
					$subQuestions = $this->getQuestionExtService()->findQuestionsByParentId($question['id']);
					foreach ($subQuestions as $subQuestion) {
						$items[] = array('questionId' => $subQuestion["id"], 'score' => 0);
					}
				}   
			}

			$this->getTestpaperService()->addTestpaperItem($testpaper['id'], $items);
			$detail = $this->getPaperDetail($testpaperId);
			return $this->createJsonResponse(array("status"=>"true","data"=>$detail));
		}

		return new JsonResponse("添加失败！");
	}

	public function deleteQuestionAction(Request $request, $testpaperId,$questionId){
		$testpaper = $this->getTestpaperService()->getTestpaper($testpaperId);
		if (empty($testpaper)) {
			return $this->createJsonResponse(array("status"=>"false","data"=>"找不到试卷！"));
		}

		$this->getTestpaperService()->deleteTestpaperItemByQuestionId($testpaper['id'], $questionId );
		
		$detail = $this->getPaperDetail($testpaperId);
		return $this->createJsonResponse(array("status"=>"true","data"=>$detail));
	}
	
	public function deleteQuestionByTypeAction(Request $request, $testpaperId,$questiontype){
		$testpaper = $this->getTestpaperService()->getTestpaper($testpaperId);
			if (empty($testpaper)) {
			return $this->createJsonResponse(array("status"=>"false","data"=>"找不到试卷！"));
		}elseif ($testpaper["status"] == "open"){
			return $this->createJsonResponse(array("status"=>"false","data"=>"试卷已使用，不能清空！"));
		}
		if($questiontype == "0"){
			$this->getTestpaperService()->reSetTestpaperById($testpaperId );
		}
		else {
			$this->getTestpaperService()->deleteTestpaperItemByQuestionType($testpaper['id'], $questiontype );
		}
		
		$detail = $this->getPaperDetail($testpaperId);
		return $this->createJsonResponse(array("status"=>"true","data"=>$detail));
	}
	
	public function questionCartAction($testpaperId){
		$detail = $this->getPaperDetail($testpaperId);
		return $this->createJsonResponse($detail);
	}
	
	public function getPaperDetail($testpaperId){
		$testpaper = $this->getTestpaperService()->getTestpaper($testpaperId);
		$paperQuestions = $this->getTestpaperService()->getTestpaperItems($testpaper['id']);
		
		$questionTypes = $this->get('topxia.twig.web_extension')->getDict('questionType');
		$types = array();
		$totalCount = 0;
		foreach($questionTypes as $key=>$val)
		{
			$count = 0;
			foreach ($paperQuestions as $question){
				if($question["questionType"] == $key && $question["parentId"] == 0){
					$count ++;
				}
			}
			$totalCount += $count;
			$type = array("id"=>$key,"name"=>$val,"count"=>$count);
			$types[] = $type;
		}

		$detail = array("totalCount"=>$totalCount,"types"=>$types);
		return $detail;
	}
	
	public function sortTestpaperAction($testpaperId){
		$questionTypes = $this->get('topxia.twig.web_extension')->getDict('questionType');
		 $this->getTestpaperService()->sortTestpaper($testpaperId,$questionTypes);
		return $this->createJsonResponse(array("status"=>"true","data"=>"排序成功"));
	}
	
	private function getQuestionExtService()
	{
		return $this->getServiceKernel()->createService('Topxia:Question.QuestionService');
	}
	
	private function getTestpaperService()
	{
		return $this->getServiceKernel()->createService('Custom:Testpaper.TestpaperService');
	}
}